﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class RatingDataProvider : DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="RatingDataProvider" /> class.
        /// </summary>
        public RatingDataProvider() { }
        #endregion

        #region [Public Methods]

        /// <summary>
        /// Gets the article rating by article id
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetRatingByArticleID(int articleID)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT ROUND(AVG(ArticleRating.Rating)) AS ArticleRating");
                base.CommandTextBuilder.Append(@" FROM (Article INNER JOIN
                         ArticleRating ON Article.ArticleID = ArticleRating.ArticleID)");
                base.CommandTextBuilder.Append(@" WHERE (Article.IsActive = True) AND (ArticleRating.IsActive = True) AND (Article.ArticleID = @ArticleID)");

                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetRatingArticleID.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// The method inserts rating
        /// </summary>
        /// <param name="articleRatingID">the article rating id</param>
        /// <param name="articleID"> the article id</param>
        /// <param name="userID">the user id</param>
        /// <param name="rating">the rating</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertRating(int articleID, int userID, int rating, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"INSERT INTO ArticleRating(ArticleID, UserID, Rating, IsActive)
                    VALUES(@ArticleID, @UserID, @Rating, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                base.DBParams.Add(@"@UserID", userID);
                base.DBParams.Add(@"@Rating", rating);
                base.DBParams.Add(@"@IsActive", isActive);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception eX)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }
        #endregion
    }
}
